<?php

namespace slinks\core\dependencyInjectionExt\compiler;

use slinks\core\DependencyInjection\Definition;
use slinks\core\DependencyInjection\Exception\InvalidArgumentException;

/**
 * Class simplifies processing of only Definitions implementing a given interface. 
 * 
 * @author Navid Mitchell
 *
 */
abstract class AbstractInterfaceProcessingPass extends AbstractDefinitionProcessingPass {

    private $class;

    /**
     * 
     * @param string $class the interface class that you wnat to process.
     */
    function __construct($class) {
        $this->class = $class;
        parent::__construct(true);
    }

    /**
     * 
     * @param string $id the service definition id; 
     * @param Definition $definition
     * @return Boolean true if processDefinition should be called for the given definition.
     */
    public function shouldProcessDefinition($id, Definition $definition) {
        $ret = false;
        $defClass = $definition->getClass();


        if (class_exists($defClass)) {

            $reflection = new \ReflectionClass($defClass);

            $ret = $reflection->isSubClassOf($this->class) || $defClass === $this->class;
        }

        return $ret;
    }

}